// miniprogram/pages/liveroom/index.js

const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
      liveroom:undefined,
      islivepush:false,
      timer: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var liveroom = this.selectComponent("#liveroom");
      this.setData({
        liveroom:liveroom
      });
      wx.setNavigationBarTitle({
        title: '直播间测试',
      })
      var that = this;
      that.getliveroom();
      that.data.timer = setInterval(function(){
        console.log(app.globalData.refreshtime/10+"秒了-------");
        that.getliveroom();
      },app.globalData.refreshtime/10)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timer);
    this.exitliveroom();

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  getliveroom:function(){
    
    var that =this;
  
      wx.request({
        url: app.globalData.interfaceWehchatliveroom,
        data: {
          method:'list' //0 位查询所有排名  top5时用5 
        },
        method:'POST',
        header: {
          'content-type': 'application/json'
        },
        success(res) {
           
         that.data.liveroom._list(res.data);

          
          console.log(res.data);
       
         
        },
        fail(res){
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        },
        complete(){
          wx.hideLoading()
        }
      })
  },

  loginliveroom:function(){
    
    var that =this;
     wx.request({
        url: app.globalData.interfaceWehchatliveroom,
        data: {
          method:'login', //login 登陆
          openid:app.globalData.openid,
          username:app.globalData.username,
        },
        method:'POST',
        header: {
          'content-type': 'application/json'
        },
        success(res) {
           
          console.log(res.data);
          if(res.data!= ''){
          that.data.liveroom.openlive(res.data);
          
          }
       
         
        },
        fail(res){
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        },
        complete(){
          wx.hideLoading()
        }
      })
  },
  exitliveroom:function(){
    
    var that =this;
     wx.request({
        url: app.globalData.interfaceWehchatliveroom,
        data: {
          method:'exit', //login 登陆
          openid:app.globalData.openid,
          username:app.globalData.username,
        },
        method:'POST',
        header: {
          'content-type': 'application/json'
        },
        success(res) {
           
         
          that.data.liveroom._exit();
          //wx.navigateBack();
          console.log(res.data);
       
         
        },
        fail(res){
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        },
        complete(){
          wx.hideLoading()
        }
      })
  },
  _login:function()
  {
    console.log("_login");
      this.loginliveroom();

  },
  _exit:function(){
    console.log("_exit");
    this.exitliveroom();
  },
  statechange(e) {
    console.log('live-player code:', e.detail.code)
  },
  error(e) {
    console.error('live-player error:', e.detail.errMsg)
  },
})